package com.hqm.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hqm.meta.Product;
import com.hqm.meta.User;
import com.hqm.service.ProductService;
import com.hqm.service.TransactionService;

@Controller
public class PageController {
	private static final Log logger = LogFactory.getLog(PageController.class);
	
	@Resource
	private ProductService productService;
	
	@Resource
	TransactionService transactionService;
	
	/**
	 * 处理首页请求
	 * 
	 * @param model
	 * @param session
	 * @param type
	 * @return 首页视图名称
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String toIndexPage( Model model, HttpSession session,
			@RequestParam(value="listType", required=false) Integer type) {
		logger.info("toIndexPage called");
		
		User user = (User)session.getAttribute("user");
		List<Product> productList = productService.getProducts();
		List<Product> products = new ArrayList<Product>();
	
		/* 对待展示在首页的商品进行遍历
		 * 判断是否售出（对卖家而言），是否已购买（对买家而言）
		 * */
		for (Product product : productList) {
			if (user != null) {
				if (transactionService.isSell(product)) {
					product.setIsSell((byte)1);
					product.setSaleNum(transactionService.getProductSoldNum(product));
				} else {
					product.setIsSell((byte)0);
				}
				if (transactionService.isBuy(product, user)) {
					product.setIsBuy((byte)1);
				} else {
					product.setIsBuy((byte)0);
				}
			}
			products.add(product);
		}
		
		//添加模板数据
		model.addAttribute("user", user);
		model.addAttribute("productList", products);
		if (type == null) {
			model.addAttribute("listType", 0);
		} else {
			model.addAttribute("listType", type);
		}
		
		return "index";
	}
	
	/**
	 * 跳转到登录页
	 * @return 登录页视图名称
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String toLogIn() {
		logger.info("toLogIn called");
		return "login";
	}
	
	/**
	 * 跳转到商品发布页
	 * 只有登录的卖家才能访问
	 * @param session
	 * @return
	 *
	 */
	@RequestMapping(value = "/public", method = RequestMethod.GET)
	public String toPublicPage(HttpSession session) {
		logger.info("toPublicPage called");
		
		
		User user = (User)session.getAttribute("user");
		
		if (user != null && user.getUsertype() == 1) {
			return "public";
		} else {
			return toLogIn();
		}
	}
	
	/**
	 * 跳转到商品编辑页
	 * 只有登录的卖家才能访问
	 * @param session
	 * @param model
	 * @param productId
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String toEditPage(HttpSession session, Model model,
			@RequestParam("id") Integer productId) {
		logger.info("toEditPage called");
		
		User user = (User)session.getAttribute("user");
		Product product = productService.getProduct(productId);
		
		if (user != null && user.getUsertype() == 1) {
			model.addAttribute("product", product);
			return "edit";
		} else {
			return toLogIn();
		}
	}
}
